home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 10 Scripting / 01 Berger / scc-parser.y < prev    next >
Encoding:
Lex Description  |  2001-10-16  |  3.8 KB  |  103 lines

  1. %{
  2. // This file contains all of the rules for GNU Bison.  This grammar defines a
  3. // very simple language that can only understand the very basic match
  4. // instructions.  Nevertheless, this still shows the basics of code generation.
  5.  
  6. #include "SCC.H"
  7. #include "PTNode.H"
  8. #include "CodeGen.H"
  9.  
  10. %}
  11.  
  12.  
  13. // These two lines defines the order of precidence for the operators.  The
  14. // first line has the lowest precidence and the last line has the highest.
  15. // Operators on the same line have the same precidence, and these operators
  16. // associate to the left.  For example, the following expressions are
  17. // equivalent:
  18. //
  19. //    2 + 3 + 5 + 7
  20. //    ( (2 + 3) + 5) + 7
  21. //
  22. // This defines the operator precidence as you would expect.
  23. %left '+' '-'
  24. %left '*' '/'
  25.  
  26.  
  27. // Define the set of tokens that the lexer will give the parser.  Currently,
  28. // the only token that the parser understands is a number.  A more robust
  29. // compiler would understand identifiers, keywords, and other tokens.
  30. %token NUMBER
  31.  
  32.  
  33. %%
  34.  
  35.  
  36. // This rule is where Bison starts (since this is the first rule), and it
  37. // defines what a valid source file.  For this sample, a source file is simply
  38. // a list of statements.
  39. file : stmtList
  40.          {
  41.            // This block of code will be executed once the statement list is
  42.            // "reduced" or completely matched.  Since this is the first rule
  43.            // that Bison started with, parsing of the source script has been
  44.            // completed.  We dump the parse tree that was built up, and then
  45.            // run the code generator over this parse tree.
  46.  
  47.            // Note that the $1, $2, etc match the "return results" of the of
  48.            // the rule's arguments (e.g., the right side of the colon).  $1 in
  49.            // this rule's case is the result of 'stmtList'.  Rules can set
  50.            // their return values by setting $$.
  51.  
  52.            $1->Dump();
  53.  
  54.            CodeGen cg;
  55.            cg.Gen( $1 );
  56.          }
  57.      ;
  58.  
  59.  
  60. // This rule defines a list of one or more statements.  Notice how this rule
  61. // refers to itself recusively.  The second part of this rule (just the single
  62. // 'stmt') will always be matched first.  This little bit of code handles
  63. // creating a block parse tree node that will contain the entire list of
  64. // statements.  If there are any additional statements, they will be matched
  65. // by the first part of this rule (notice how its code adds the new statement
  66. // to the end of block node).
  67. stmtList : stmtList stmt           { $$ = $1;  $$->Add( $2 );  }
  68.          | stmt                    { $$ = new BlockNode( $1 ); }
  69.          ;
  70.  
  71.  
  72. // This rule defines what a statement is.  For this sample, a statement is
  73. // simply an expression that is terminated by a semi-colon.  In a more complex
  74. // language, other statements would be if statements, while loops, for loops,
  75. // etc.
  76. stmt : expr ';'                    { $$ = new StatementNode( $1 ); }
  77.      ;
  78.  
  79.  
  80. // This rule defines what an expression.  An expression can be a single number
  81. // (the last rule), or it can be any list basic math instructions.  Each of
  82. // these rules handles creating the proper parse tree node with the operands.
  83. expr : expr '+' expr               { $$ = new AddNode( $1, $3 );      }
  84.      | expr '-' expr               { $$ = new SubtractNode( $1, $3 ); }
  85.      | expr '*' expr               { $$ = new MultiplyNode( $1, $3 ); }
  86.      | expr '/' expr               { $$ = new DivideNode( $1, $3 );   }
  87.      | '(' expr ')'                { $$ = $2;                         }
  88.      | NUMBER                      { $$ = $1;                         }
  89.      ;
  90.  
  91.  
  92. %%
  93.  
  94.  
  95. // This function is required to be defined by Bison.  It is called whenever
  96. // any kind of error is raised while parsing the input stream.
  97. int yyerror( char *err )
  98. {
  99.   puts( err );
  100.  
  101.   return 0;
  102. }
  103.